#include <QFileDialog>
#include "DlgDebug.h"
#include "scImpl.h"

#define FCT "DlgDebug::DlgDebug"
DlgDebug::DlgDebug(QWidget *parent) : QDialog(parent)
{
	ui.setupUi(this);
	Trc * trc=Trc::inst();
	trc->getAllLevels(levels);
	for(int i=0; i<(int)levels.size(); ++i)
	{
		ui.traceLevel->addItem(QString(tr(levels.at(i).second.c_str())), QVariant(i));
		if(levels.at(i).first==trc->getLevel())
			ui.traceLevel->setCurrentIndex(i);
	}
	trc->getAllDests(dests);
	for(int i=0; i<(int)dests.size(); ++i)
	{
		ui.traceLocation->addItem(QString(tr(dests.at(i).second.c_str())), QVariant(i));
		if(dests.at(i).first==trc->getDest())
			ui.traceLocation->setCurrentIndex(i);
	}
	ui.fileName->setText(QString(trc->getFileDest().c_str()));
	ui.fileName->setEnabled(trc->getDest()==Trc::FILE);
	ui.browseTraceFile->setEnabled(trc->getDest()==Trc::FILE);
	ui.appCodes->setText(trc->getAppCodesFlat().c_str());
	ui.subCodes->setText(trc->getSubCodesFlat().c_str());

	//connect(ui.traceLevel, SIGNAL(currentIndexChanged (int)), this, SLOT(levelSelectSl(int)));
	connect(ui.traceLocation, SIGNAL(currentIndexChanged (int)), this, SLOT(destSelectSl(int)));
	connect(ui.browseTraceFile, SIGNAL(clicked()), this, SLOT(browseTraceFileSl()));
}

#undef FCT
#define FCT "DlgDebug::~DlgDebug"
DlgDebug::~DlgDebug()
{
}

#undef FCT
#define FCT "DlgDebug::accept"
void DlgDebug::accept ()
{
	Trc::Level level=levels.at(ui.traceLevel->currentIndex()).first;
	Trc::Dest dest=dests.at(ui.traceLocation->currentIndex()).first;
	Trc * trc=Trc::inst();
	ctrc(Trc::NONE,APP,FCT)<<"level "<<level<<" destination "<<dest<<endl;
	Trc::inst()->setLevel(level);
	if(dest==Trc::FILE)
		trc->setFileDest(ui.fileName->text().toStdString().c_str());
	else
		Trc::inst()->setDest(dest);
	trc->clearAppCodes();
	trc->clearSubCodes();
	trc->addAppCodes(ui.appCodes->text().toStdString().c_str());
	trc->addSubCodes(ui.subCodes->text().toStdString().c_str());
	QDialog::accept();
}

#undef FCT
#define FCT "DlgDebug::destSelectSl"
void DlgDebug::destSelectSl(int index)
{
	Trc::Dest dest=dests.at(index).first;
	if(dest==Trc::FILE)
	{
		ui.fileName->setEnabled(true);
		ui.browseTraceFile->setEnabled(true);
	}
	else
	{
		ui.fileName->setEnabled(false);
		ui.browseTraceFile->setEnabled(false);
	}
}

#undef FCT
#define FCT "DlgDebug::browseTraceFileSl"
void DlgDebug::browseTraceFileSl()
{
	QString fileName=QFileDialog::getOpenFileName(this, tr("Open Trace File"));
	if(!fileName.isEmpty()) ui.fileName->setText(fileName);
}
